package com.FrameWork.Common;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.FrameWork.DB.DataProvider;
import com.FrameWork.Exception.CrashHandler;
import com.FrameWork.Synchoronization.SyncMonitor;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;


public class MyApplication extends Application {

	public List<Activity> activityList = new LinkedList<Activity>();
	private static MyApplication instance;
	//百度MapAPI的管理类
	public BMapManager mBMapMan = null;
	// 授权Key
	// 申请地址：http://dev.baidu.com/wiki/static/imap/key/
	public String mStrKey = "B5FF5CFD024E37968E382D01E1A2F076E47048F1";

	

	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	public static class MyGeneralListener implements MKGeneralListener {
		@Override
		public void onGetNetworkState(int iError) {
			Toast.makeText(MyApplication.getInstance().getApplicationContext(), "您的网络出错啦！",
					Toast.LENGTH_LONG).show();
		}

		@Override
		public void onGetPermissionState(int iError) {
			
		}
	}
	@Override
	//建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
	public void onTerminate() {
		// TODO Auto-generated method stub
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onTerminate();
	}


	@Override
	public void onCreate()
	{
		super.onCreate();
	
		
		mBMapMan = new BMapManager(this);
		mBMapMan.init(this.mStrKey, new MyGeneralListener());
		mBMapMan.getLocationManager().setNotifyInternal(1, 1);
		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(this);
	}
	
	
	public static MyApplication getInstance() {
		return instance;
	}

	public static void setInstance(MyApplication app) {
		instance = app;
	}

	public void addActivity(Activity activity) {
		activityList.add(activity);
	}

	public void exit() {
		for(int i=activityList.size()-1;i>=0;i--)
		{
			activityList.get(i).finish();
		}
		if(mBMapMan != null)
			mBMapMan.destroy();
		
		System.exit(0);
	}

	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
		Log.w("W", "onLowMemory");
	}

	// /全局数据访问对象
	public DataProvider DataProvider;
	public SyncMonitor SyncMonitor = null;

}
